package com.iss.yunpan.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iss.yunpan.entity.FileContent;
import com.iss.yunpan.service.FileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class FileController {

    @Autowired
    private FileService service;

    @RequestMapping("getAllFile")
    @ResponseBody//object是所有类型中的父类。只要不是form表单提交的，都需要加本行
    public Map<String, Object> getAllFile(String id, int page, int limit,String parentId){  //修改？
        System.out.println(id);
        if(parentId==null) {
            parentId = "0";
        }
        Map<String, Object>map=new HashMap<>();
        PageHelper.startPage(page,limit);
        List<FileContent> list=service.getAllFile(id);                              //(id,parentId)
        PageInfo<FileContent> pageInfo =new PageInfo<FileContent>(list);
        System.out.println(pageInfo);
        map.put("code",0);//详见layui数据表格文档示例！
        map.put("msg","");
        map.put("count",pageInfo.getTotal());//获取总个数
        map.put("data", pageInfo.getList());//最终的数据
        return map;
    }

    @RequestMapping("insertFolder")
    @ResponseBody   //ajax要加上这玩意儿！
    public Map<String,Object>insertFolder(String fileName,Integer parentId)
    {
        Map<String, Object>map=new HashMap<>();
        if(parentId==null)
        {

            parentId=0;
        }
        if(fileName.equals(""))
        {map.put("msg","文件夹名称不能为空");}
        else
        {
            boolean flag=service.checkFileContent(fileName,parentId);
            if (flag)
            { map.put("msg","文件夹名字不能重复！");}
            else{
                service.insertFolder(fileName,parentId);
                map.put("code",0);
                map.put("","文件夹新建成功");
            }
        }
        return  map;
    }

    @RequestMapping("uploadFile")
    @ResponseBody//空跳转
    public Map<String,Object> uploadFile(@RequestParam("file") MultipartFile file, String parentId, HttpServletRequest request) throws IOException {//map为springmvc上传内容类型,获取上传文件file信息，“file”为底层所传
        Map<String, Object>map=new HashMap<>();
        if(parentId==null) {
            parentId = "0";//先验证
        }
        if (file != null) {
            String fileName = file.getOriginalFilename();//获取文件名
            InputStream input = file.getInputStream();//获取文件内容，该两项为向底层所传内容
            service.uploadFile(fileName, input, parentId);//传此3个参数
            upload(file);
            map.put("code", 0);//使判断上传成功
            map.put("msg", "上传成功");
        }
        else{//file==null
            map.put("meg","文件不能为空");
        }
        return map;
    }

    private void upload(MultipartFile file) throws IOException {

        File destFile = new File(ResourceUtils.getURL("classpath:").getPath());
        String path = destFile.getAbsolutePath()+"\\static\\upload";
        String filename = file.getOriginalFilename();
        File filepath = new File(path,filename);
        if(!filepath.exists()) {
            filepath.mkdirs();
        }
        //判断路径是否存在，如果不存在就创建一个
        if (!filepath.getParentFile().exists()) {
            filepath.getParentFile().mkdirs();
        }
        //将上传文件保存到一个目标文件当中
        file.transferTo(new File(path + File.separator + filename));
    }
}
